package com.example.midushop.controller;

import com.example.midushop.entity.product.sku.ProductSKU;
import com.example.midushop.service.product.impl.*;
import com.example.midushop.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 商品操作API接口控制器
 * */
@RestController
@RequestMapping("/api/product")
public class ProductController {
    @Autowired
    ProductSPUServiceImpl productSPUService;
    @Autowired
    SPUImageServiceImpl spuImageService;
    @Autowired
    SPU_detailServiceImpl spu_detailService;
    @Autowired
    AttributeServiceImpl attributeService;
    @Autowired
    SPU_AttrServiceImpl spu_attrService;
    @Autowired
    ProductSKUServiceImpl productSKUService;
    @Autowired
    SKU_AttrServiceImpl sku_attrService;
    @Autowired
    AttrValueServiceImpl attrValueService;

    /**
     *添加商品SPU信息
     * @param categorythree_id 商品类目id
     * @param name SPU名
     * @param code 商品编号
     * @param cover 封面
     * @param intro 商品介绍
     * @param brand_id 品牌ID
     * @param after_sales_service 售后服务
     * @return 返回添加完成的对象
     */
    @Value("${prop.up-folder-BrandImage}")
    String up_folder;
    @PostMapping("/addProductSPUAll")
    public R addProductSPUAll(@RequestParam(required = false)long categorythree_id,
                              @RequestParam(required = false,defaultValue = "") String name,
                              @RequestParam(required = false,defaultValue = "")String code,
                              @RequestPart("file") MultipartFile[] findImage,
                              @RequestParam(required = false,defaultValue = "")String intro,
                              @RequestParam(required = false)int brand_id,
                              @RequestParam(required = false,defaultValue = "")String after_sales_service) throws IOException {
        if(findImage.length>0){
            //遍历文件
            for (MultipartFile multipartFile : findImage) {
                if(!multipartFile.isEmpty()){
                    String originalFilename = multipartFile.getOriginalFilename();
                    String newname= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
                    //文件保存路径
                    String path=up_folder+"\\"+newname;
                    multipartFile.transferTo(new File(path));
                    //存入数据库
                    Date day = new Date();
                    if(code.equals("null") || code.equals("")){
                        code = UUID.randomUUID().toString();
                    }
                    productSPUService.addProductSPU(categorythree_id,name,code,newname,intro,brand_id,after_sales_service,1,0,2,day,day);
                }
            }
        }
        return R.ok(productSPUService.findProductAll(0,categorythree_id,name,code,brand_id));
    };

    /**
     *多条件查询h商品SPUID
     * @param spu_id 商品SPU_ID
     * @param categorythree_id 三级分类ID
     * @param name 商品SPU名称
     * @param brand_id 品牌ID
     * @return 返回查询结果集合
     */
    @GetMapping("/findProductAll/spu_id={spu_id}")
    public R findProductAll(@PathVariable("spu_id")long spu_id,@RequestParam(required = false)long categorythree_id,
                            @RequestParam(required = false,defaultValue = "")String name,
                            @RequestParam(required = false,defaultValue = "")String code,
                            @RequestParam(required = false)long brand_id){
        return R.ok(productSPUService.findProductAll(spu_id,categorythree_id,name,code,brand_id));
    }
    @GetMapping("/findProductAllInfo/spu_id={spu_id}")
    public R findProductAllInfo(@PathVariable("spu_id")long spu_id,@RequestParam(required = false)long categorythree_id,
                            @RequestParam(required = false,defaultValue = "")String name,
                            @RequestParam(required = false,defaultValue = "")String code,
                            @RequestParam(required = false)long brand_id){
        return R.ok(productSPUService.findProductAllInfo(spu_id,categorythree_id,name,code,brand_id));
    }

    /**
     * 添加商品SPU图片集合
     * @param spu_id SPUID
     * @param findImageList 图片文件集
     * @param sort 排序
     * @return 返回受影响行数
     */
    @Value("${prop.SPUImage}")
    String SPUImage;
    @PostMapping("/addSPUImage/spu_id={spu_id}")
    public R addSPUImage(@PathVariable("spu_id") long spu_id,@RequestPart("file") MultipartFile[] findImageList) throws IOException {
        int a = 0;
        System.out.println(findImageList.length);
        if(findImageList.length>0){
            //遍历文件
            for (MultipartFile multipartFile : findImageList) {
                if(!multipartFile.isEmpty()){
                    String originalFilename = multipartFile.getOriginalFilename();
                    String newname= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
                    //文件保存路径
                    String path=SPUImage+"\\"+newname;
                    multipartFile.transferTo(new File(path));
                    //存入数据库
                    spuImageService.addSPUImage(spu_id,newname,2);
                    System.out.println("进来了");
                    a ++ ;
                }
            }
        }
        return R.ok("成功添加"+a+"个图片");
    };

    /**
     *根据商品SPU——ID查询商品图片集
     * @param spu_id 商品SPU——ID
     * @param id 图片ID
     * @return 返回该商品的图片集
     */
    @GetMapping("/findProductSPU_image/spu_id={spu_id}&&id={id}")
    public R findProductSPU_image(@PathVariable("spu_id")long spu_id,@PathVariable("id")long id){
        return R.ok(spuImageService.findProductSPU_image(spu_id,id));
    };

    /**
     * 根据回传的spu_id，添加商品富文本介绍
     * @param spu_id 商品Spu_id
     * @param contentValue 富文本内容
     * @return 返回受影响行数
     */
    @PostMapping("/addSPU_detail/spu_id={spu_id}")
    public R addSPU_detail(@PathVariable("spu_id")long spu_id,@RequestParam(required = false,defaultValue = "") String contentValue){
        System.out.println(contentValue);
        return R.ok(spu_detailService.addDetail(spu_id,contentValue));
    }

    /**
     * 根据SPU_id查询商品富文本介绍内容
     * @param spu_id spu_id
     * @return 返回SPU_detail实体对象
     */
    @GetMapping("/findSPU_detail/spu_id={spu_id}")
    public R findSPU_detail(@PathVariable("spu_id")long spu_id){
        return R.ok(spu_detailService.findSPU_detail(spu_id));
    }

    /**
     * 根据属性编号或名称查询，不传是查询所有
     * @param attr_id 属性ID
     * @param name  属性名称
     * @return 返回attr集合
     */
    @GetMapping("/findAttribute/attr_id={attr_id}")
    public R findAttribute(@PathVariable("attr_id")long attr_id,@RequestParam(required = false,defaultValue = "")String name){
        return  R.ok(attributeService.findAttribute(attr_id,name));
    };

    /**
     * 添加商品SPU属性值
     * @param spu_id 商品SPU_ID
     * @param attr_id 属性ID
     * @param value2 属性值
     * @return 返回受影响行数
     */
    @PostMapping("/addSPU_AttrOne")
    public R addSPU_Attr(@RequestParam(required = false) long spu_id,
                         @RequestParam(required = false) long attr_id,@RequestParam(required = false,defaultValue = "")String value2){
        return R.ok(spu_attrService.addSPU_Attr(spu_id,attr_id,value2,1));
    }
    @PostMapping("/addSPU_Attr")
    public R addSPU_Attr2(@RequestParam(required = false) long spu_id,
                          @RequestParam(required = false) long attr_id,
                          @RequestPart("file") MultipartFile[] findImage) throws IOException {
        if(findImage.length>0){
            //遍历文件
            for (MultipartFile multipartFile : findImage) {
                if(!multipartFile.isEmpty()){
                    String originalFilename = multipartFile.getOriginalFilename();
                    String newname= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
                    //文件保存路径
                    String path=up_folder+"\\"+newname;
                    multipartFile.transferTo(new File(path));
                    return R.ok(spu_attrService.addSPU_Attr(spu_id,attr_id,newname,1));
                }
            }
        }
        return R.ok("添加不成功");
    }
    /**
     *添加商品SKU
     * @param spu_id 商品SPU_ID
     * @param sku_name 商品SKU名称
     * @param code 规格编号
     * @param intro 规格介绍
     * @param quantity 库存数量
     * @param price 销售价格
     * @param SKUCover SKU封面
     * @return 返回添加成功后的SKU对象
     */
    @PostMapping("/addProductSKU/{spu_id}/{quantity}")
    public R addProductSKU(@PathVariable("spu_id") long spu_id,@RequestParam(required = false,defaultValue = "")String sku_name,
                           @RequestParam(required = false,defaultValue = "")String code,@RequestParam(required = false,defaultValue = "")String intro,
                           @PathVariable("quantity") int quantity,@RequestParam(required = false) double price,
                           @RequestPart("file") MultipartFile[] SKUCover) throws IOException {
        if(SKUCover.length>0){
            //遍历文件
            for (MultipartFile multipartFile : SKUCover) {
                if(!multipartFile.isEmpty()){
                    String originalFilename = multipartFile.getOriginalFilename();
                    String newname= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
                    //文件保存路径
                    String path=SPUImage+"\\"+newname;
                    multipartFile.transferTo(new File(path));
                    //存入数据库
                    productSKUService.addProductSKU(spu_id,sku_name,code,intro,0,quantity,price,newname,1,0,new Date(),new Date());
                }
            }
        }
        return R.ok(productSKUService.findProductSKU(0,spu_id,sku_name,code,0));
    }

    /**
     * 多条件查询商铺SKU
     * @param sku_id 商品SKU_id
     * @param spu_id 商品SPU_id
     * @param sku_name 商品sku_name
     * @param code 商品规格编号
     * @return List对象集合
     */
    @GetMapping ("/findProductSKU/sku_id={sku_id}&&spu_id={spu_id}")
    public R findProductSKU(@PathVariable("sku_id")long sku_id,@PathVariable("spu_id")long spu_id,
                            @RequestParam(required = false,defaultValue = "")String sku_name,@RequestParam(required = false,defaultValue = "")String code){
        return R.ok(productSKUService.findProductSKU(sku_id,spu_id,sku_name,code,0));
    }
    //分页查询SKU
    @GetMapping ("/findProductSKUTwo")
    public PageInfo<ProductSKU> findProductSKUTwo(@RequestParam(required = false,defaultValue = "")long sku_id,@RequestParam(required = false,defaultValue = "")long spu_id, @RequestParam(required = false,defaultValue = "")String sku_name,
                                                  @RequestParam(required = false,defaultValue = "")String code,@RequestParam(required = false,defaultValue = "")int pageNum,@RequestParam(required = false,defaultValue = "5") int siz,@RequestParam(required = false,defaultValue = "") int status){
        System.out.println(pageNum+"  and "+siz);
        return productSKUService.findProductSKUTwo(sku_id,spu_id,sku_name,code,pageNum,siz,status);
    }
    //多条件分页查询SKU
    @GetMapping ("/findProductSKUThree")
    public PageInfo<ProductSKU> findProductSKUThree(@RequestParam(required = false,defaultValue = "") String sku_id,@RequestParam(required = false,defaultValue = "")long spu_id, @RequestParam(required = false,defaultValue = "")String sku_name,
                                                  @RequestParam(required = false,defaultValue = "")String code,@RequestParam(required = false,defaultValue = "")int pageNum,@RequestParam(required = false,defaultValue = "5") int siz,@RequestParam(required = false,defaultValue = "") int status){
        System.out.println(pageNum+"  and "+siz);
        String[] attr = new String[0];
        if(sku_id.equals("[]") || sku_id.equals("")){
            attr = new String[1];
            attr[0] ="0";
        }else{
            attr = sku_id.split(",");
        }
        long[] arr = new long[attr.length];
        for (int i = 0; i < attr.length; i++) {
            arr[i] = Long.parseLong(attr[i]);
        }
        return productSKUService.findProductSKUThree(arr,spu_id,sku_name,code,pageNum,siz,status);
    }

    /**
     *添加商品属性参数
     * @param sku_id 商品SKU_ID
     * @return 返回受影响行数
     */
    @PostMapping("/addSKU_Attr/sku_id={sku_id}")
    public R addSKU_Attr(@PathVariable("sku_id") long sku_id){
        return R.ok(sku_attrService.addSKU_Attr(sku_id));
    }

    /**
     * 查询商品SKU属性
     * @param sku_id
     * @param id
     * @return
     */
    @GetMapping("/findSKU_Attr/sku_id={sku_id}&&id={id}")
    public R findSKU_Attr(@PathVariable("sku_id")long sku_id,@PathVariable("id") long id){
        return R.ok(sku_attrService.findSKU_Attr(sku_id,id));
    }

    /**
     * SKU商品规格参数值绑定
     * @param pro_sku_attr_id SKU属性ID
     * @param pro_spu_attr_id SPU规格ID
     * @return 返回受影响行数
     */
    @PostMapping("/addAttrValue/pro_sku_attr_id={pro_sku_attr_id}&&pro_spu_attr_id={pro_spu_attr_id}")
    public R addAttrValue(@PathVariable("pro_sku_attr_id")long pro_sku_attr_id,@PathVariable("pro_spu_attr_id")long pro_spu_attr_id){
        return R.ok(attrValueService.addAttrValue(pro_sku_attr_id,pro_spu_attr_id));
    };
    @GetMapping("/findAttrValue/pro_sku_attr_id={pro_sku_attr_id}&&pro_spu_attr_id={pro_spu_attr_id}")
    public R findAttrValue(@PathVariable("pro_sku_attr_id")long pro_sku_attr_id,@PathVariable("pro_spu_attr_id")long pro_spu_attr_id){
        return R.ok(attrValueService.getAttrValue(pro_sku_attr_id,pro_spu_attr_id));
    }

    /**
     * 多条件查询商品属性
     * @param spu_id 商品ID
     * @param value2 属性值
     * @return list集合
     */
    @GetMapping("/findSPU_Attr/{spu_id}")
    public R findSPU_Attr(@PathVariable("spu_id") long spu_id,@RequestParam(required = false,defaultValue = "") String value2){
        System.out.println(value2);
        return R.ok(spu_attrService.findSPU_Attr(0,spu_id,0,value2));
    }

    @GetMapping("/findSPU_AttrTwo/{spu_id}/{attr_id}")
    public R findSPU_AttrTwo(@PathVariable("spu_id") long spu_id,@PathVariable("attr_id") long attr_id){
        return R.ok(spu_attrService.findSPU_Attr(0,spu_id,attr_id,""));
    }
}
